package com.shop.util;

import org.apache.log4j.Logger;

import com.g3net.tool.Path;
import com.g3net.tool.ReadXML;

/**
 * book3.0的各服务器的公用配置
 * @author yuxiaowei
 *
 */
public class CommonConfig {
	private static Logger log = Logger.getLogger(CommonConfig.class);
	private static ReadXML r=null;
	public static void refresh(){
		r=null;
	}
	/**
	 * 读取给定的文件,转换为相应格式的配置数据
	 * @param fileName	配置文件全路径
	 * @return
	 */
	public static String[] readFile(String fileName)
	{
		try{
			if(r==null)
			{
				r=new ReadXML(fileName);
			}
			return r.readContentByTagName("server");
		}catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	/**
	 * 读取给定的文件,转换为相应格式的配置数据
	 * @param fileName	配置文件全路径
	 * @return
	 */
	public static String readValue(String fileName,String tagName)
	{
		try{
			if(r==null)
			{
				r=new ReadXML(fileName);
			}
			return r.readValueByTagName(tagName);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
//	/**
//	 * 
//	 * @param fileName
//	 * @return
//	 */
//	public static String[] readCServers(String fileName)
//	{
//		try{
//			ReadXML r=new ReadXML(fileName);
//			return r.readList("cserver");
//		}catch (Exception e) {
//			// TODO: handle exception
//		}
//		return null;
//	}
//	public static String[] readFServers(String fileName)
//	{
//		try{
//			ReadXML r=new ReadXML(fileName);
//			return r.readList("fserver");
//		}catch (Exception e) {
//			// TODO: handle exception
//		}
//		return null;
//	}
	public static String[] readServers(String fileName,String name)
	{
		try{
			if(r==null)
			{
				r=new ReadXML(fileName);
			}
			return r.readContentByTagName(name);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public static String[] readValues(String fileName,String name)
	{
		try{
			if(r==null)
			{
				r=new ReadXML(fileName);
			}
			return r.readContentByTagName(name);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	public static void main(String[] args) throws Exception {
//		System.out.println(Path.getClassPath(""));
		String[] ss=readServers(Path.getClassPath("common.xml"),"lserver");
//		String tt=readValue(Path.getClassPath("common.xml"),"book-datatype");
//		String lcserver=readValue(Path.getClassPath("common.xml"),"isshanghai");
//		System.out.println(lcserver);
		for (int i = 0; i < ss.length; i++) {
			System.out.println(ss[i]);
		}
//		System.out.println(tt);
//		for (int i = 0; i < 100; i++) {
//			System.out.println(20*i+1);
//		}
//		String s="分段";
//		byte[] b=s.getBytes("iso-8859-1");
//		String ss=new String(b,"utf-8");
//		System.out.println(ss);
		
	}
}
